<template>
    <view class="page over-auto-y column">
        <hj-navbar title="首页" :isShowBack="false" />
        <!-- 内容区 -->
        <view class="flex-auto w100">
            <MainTop />
        </view>
        <!-- 地图位置 -->
        <OurLocation />
        <!-- 最新资讯 -->
        <LatestNews />
        <!-- 安全底部 -->
        <hj-safe-footer />
    </view>
</template>

<script setup>
/**
 * @description 小程序的首页
 * @author yinzhi
 * @date 2025-03-07
 */
import { onLoad, onShareAppMessage, onShareTimeline } from '@dcloudio/uni-app'
import { computed, defineComponent } from 'vue'
import { useStore } from 'vuex'
import LatestNews from './components/LatestNews.vue'
import OurLocation from './components/OurLocation.vue'
import MainTop from './components/MainTop.vue'
// 引入组件
defineComponent({
    components: {
        LatestNews,
        OurLocation,
        MainTop
    }
})
const store = useStore()

const userId = computed(() => store.state.platform.userInfo.id || '')

/**
 * @description 用户可能通过扫码或者分享过来的，需要在这里记住分享人的id
 * @param {Object} e 扫码携带过来的参数或者分享过来的参数
 */
onLoad(async e => {
    if (e.scene) {
        const queryArray = await decodeURIComponent(e.scene)?.split('&')
        let queryObject = {}
        queryArray.forEach(async item => {
            var keyValue = item.split('=')
            var key = await decodeURIComponent(keyValue[0])
            var value = await decodeURIComponent(keyValue[1] || '')
            queryObject[key] = value
        })
        console.log('二维码参数：', queryObject)
    }
    if (e.inviteId) {
        console.log('分享人id：', e.inviteId)
    }
})

// 点击微信小程序右上角分享
onShareAppMessage(() => {
    return {
        title: '分享了一个无人机学习机会给你，快来看看吧！',
        path: `/pages/index/index?inviteId=${userId.value}`
    }
})

// 分享到朋友圈,只能分享当前页面，不能指定页面。
onShareTimeline(() => {
    return {
        title: '分享了一个无人机学习机会给你，快来看看吧！',
        query: `inviteId=${userId.value}`
    }
})
</script>

<style scoped lang="scss">
.page {
    background: #f6f6f6;
}
</style>
